using System.Collections.Generic;

namespace HuaXianQu.ProtoBuffEx.ToolEditor
{
    public class ProtoEnumGen : IProtoType
    {
        public const string EnumFeildName = "_enum";
        private string proto;
        public string ClassName { get; set; }
        public string Parse(List<string> lines, string tableName = null)
        {
            if (lines.Count < 3)
            {
                return null;
            }

            ClassName = lines[0].Trim();
            if (string.IsNullOrEmpty(ClassName))
            {
                return null;
            }

            List<string> enumNames = new List<string>();
            List<string> enumValueNames = new List<string>();

            string value = lines[1].Trim();
            string[] matches = value.Split(':');
            if (matches.Length > 0)
            {
                enumNames.Add($"    {matches[0]} = 0;");
                enumValueNames.Add(matches[0]);
            }

            for (int i = 2; i < lines.Count; i++)
            {
                value = lines[i].Trim();
                matches = value.Split(':');
                if (matches.Length == 2)
                {
                    enumValueNames.Add(matches[0]);
                    enumNames.Add($"    {matches[0]} = {matches[1]};");
                }
            }

            CurObjectInfo.CustomVariableNameList.Add(ClassName);
            CurObjectInfo.IsEnum = true;
            CurObjectInfo.EnumValueName.AddRange(enumValueNames);
            proto = $"enum {ClassName}\n{{\n{string.Join('\n', enumNames)}\n}}";
            return proto;
        }


        public ObjectInfo CurObjectInfo { get; set; } = new ObjectInfo();
        public bool IsDataClass { get; set; }

        public override string ToString()
        {
            return proto;
        }
    }
}